package org.yy.service;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yy.dao.AuthCodeDao;
import org.yy.pojo.AuthCode;

import java.util.Date;
import java.util.List;

@Service
public class CodeService {


    @Autowired
    AuthCodeDao authCodeDao;

    public AuthCode saveCode(String email, String code) {
        AuthCode authCode = new AuthCode(email, code);
        AuthCode save = authCodeDao.save(authCode);
        return save;
    }

    public Integer validate(String email, String code) {

        Date date = new Date();
        date = DateUtils.addMinutes(date, -10);

        List<AuthCode> list = authCodeDao.findByEmailAndCodeAndExpiretimeAfter(email, code, date);
        System.out.println(list);

        if (list.size() > 0) {
            return 0;
        }
        return 1;
    }

}
